/*
 * @Author: 涵崽
 * @Date: 2024-04-10 11:52:00
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2024-06-14 15:14:48
 * @Description: 微信：1045654
 */
/*  */

import JsEncrypt from "jsencrypt"; //rsa加密
import CryptoJS from "crypto-js";
//加密方法
export function RSAencrypt(pas) {
  //实例化jsEncrypt对象
  let jse = new JsEncrypt();
  //设置公钥
  jse.setPublicKey(
    "-----BEGIN PUBLIC KEY-----\n" +
      "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1uzNojpZxuoEvntLyL8o\n" +
      "eZfa1X75gaixVvGaRe+DQ12ngpeRjqVclKhQKyWsnQT8sLSeL7O9Au1HH3MVjsDj\n" +
      "7eoNIBOFKMxk7rsLRczcBEAdDIkWzaexXcfgHtyNa9WvRQyYnmsGD9eR2epShPh2\n" +
      "u+4k/HN9HJunfL8ztXmhcfTNXzWyoXTJZci4fyTH0drGRc12wQLStsRaqx+PoJQG\n" +
      "OQQZHp2loX95ZEbx5a1lFDimXE4xcdTz3hHS667WMOg96HtutXGkaWm7zVctAvLa\n" +
      "zc6YJGrOq3uWbHEGuUqJz1mljf8isbijaLJZ7iN6V3vuYlBf2sJkGFCpRcE5UcWp\n" +
      "MwIDAQAB\n" +
      "-----END PUBLIC KEY-----"
  );
  //解密
  return jse.encrypt(pas);
}
//解密方法
export function RSAdecrypt(pas) {
  let jse = new JsEncrypt();
  jse.setPrivateKey(); //私钥
  return jse.decrypt(pas);
}
// AES加密
export function AESencrypt(sing, data) {
  let secretKey = CryptoJS.enc.Utf8.parse(sing);
  let ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), secretKey, {
    mode: CryptoJS.mode.CBC,
    iv: CryptoJS.enc.Utf8.parse("3zyJFPEzh5rUeUNi"),
    padding: CryptoJS.pad.Pkcs7,
  }).toString();
  return ciphertext;
}
//AES解密
export function AESdecrypt(sing, data) {
  let secretKey = CryptoJS.enc.Utf8.parse(sing);
  let bbb = CryptoJS.AES.decrypt(data, secretKey, {
    mode: CryptoJS.mode.CBC,
    iv: CryptoJS.enc.Utf8.parse("3zyJFPEzh5rUeUNi"),
    padding: CryptoJS.pad.Pkcs7,
  });
  return CryptoJS.enc.Utf8.stringify(bbb);
}
// 生成随机字符串
export function generateRandomString(length) {
  const characters =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  let result = "";
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}
